<template>
  <el-dialog
    title="新增角色"
    :visible="dialogVisible"
    width="30%"
    :before-close="onCancel"
  >
    <el-form ref="employRef" :model="employData" :rules="rules">
      <el-form-item label="角色名称" label-width="120px" prop="name">
        <el-input v-model="employData.name" />
      </el-form-item>
      <el-form-item label="启用" label-width="120px" prop="state">
        <el-switch
          v-model="employData.state"
          :active-value="1"
          :inactive-value="0"
        />
      </el-form-item>
      <el-form-item label="角色描述" label-width="120px" prop="description">
        <el-input v-model="employData.description" type="textarea" />
      </el-form-item>
      <el-form-item style="text-align:center">
        <el-button type="primary" @click="onSubit">确定</el-button>
        <el-button type="danger" @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>

</template>

<script>
import { addRoleApi } from '@/api/role'

export default {
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      employData: {
        name: '',
        description: '',
        state: 1
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: ['blur', 'change'] }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    onSubit() {
      this.$refs.employRef.validate(async(e) => {
        if (e) {
          await addRoleApi(this.employData)
          this.$message.success('添加成功')
          this.$emit('update:list')
          this.onCancel()
        }
      })
    },
    onCancel() {
      this.$refs.employRef.resetFields()
      this.$emit('update:dialogVisible', false)
    }
  }
}
</script>

<style>

</style>
